package com.streambolics.worseminton.servlet;

import java.util.Calendar;

import com.streambolics.std.HtmlBuilder;
import com.streambolics.worseminton.data.Event;
import com.streambolics.worseminton.data.Invitation;
import com.streambolics.worseminton.data.Location;
import com.streambolics.worseminton.data.LocationInterest;

public class Snippets
{
    public static void generateEventComment (HtmlBuilder aBuilder, Event aEvent, String aURL, String aPrompt)
    {
        aBuilder.openForm (aURL);
        aBuilder.addHidden ("ID", aEvent.getIdAsString ());
        aBuilder.addTextField ("COMMENT", "Comment:");
        aBuilder.addSubmitButton ("OK", aPrompt);
        aBuilder.closeForm ();
    }

    public static void generateEventInfo (HtmlBuilder aBuilder, Event aEvent)
    {
        Calendar c = aEvent.getCalendar ();

        aBuilder.addParagraph ("Enter the date when the event will occur:");
        aBuilder.addDateField ("DATE", "Date:", c.get (Calendar.YEAR), c.get (Calendar.YEAR) + 1, c);
        aBuilder.addRule ();

        aBuilder.addTextField ("COMMENT", "Comment:");
        aBuilder.addRule ();

        aBuilder.addParagraph ("You can enter two different times, and the names of the reserved courts.");
        aBuilder.addTimeField ("TIME1", "Time & Fields:", c);
        aBuilder.addTextField ("FIELD11", "", aEvent.getField11 (), 4);
        aBuilder.addTextField ("FIELD12", "", aEvent.getField12 (), 4);
        aBuilder.addTextField ("FIELD13", "", aEvent.getField13 (), 4);
        aBuilder.addTextField ("FIELD14", "", aEvent.getField14 (), 4);
        aBuilder.addBreak ();
        aBuilder.addTimeField ("TIME2", "Time & Fields:", null);
        aBuilder.addTextField ("FIELD21", "", aEvent.getField21 (), 4);
        aBuilder.addTextField ("FIELD22", "", aEvent.getField22 (), 4);
        aBuilder.addTextField ("FIELD23", "", aEvent.getField23 (), 4);
        aBuilder.addTextField ("FIELD24", "", aEvent.getField24 (), 4);
    }

    public static void generateInvitationComment (HtmlBuilder aBuilder, Invitation aInvitation, String aURL, String aPrompt)
    {
        aBuilder.openForm (aURL);
        aBuilder.addHidden ("ID", aInvitation.getIdAsString ());
        aBuilder.addTextField ("COMMENT", "Comment:");
        aBuilder.addTextField ("PRIVATECOMMENT", "Private comment:");
        aBuilder.addRule ();
        aBuilder.addParagraph ("The private comment will be seen by the event coordinator only.");
        aBuilder.addSubmitButton ("OK", aPrompt);
        aBuilder.closeForm ();
    }

    public static void generateInvitationList (HtmlBuilder aBuilder, Event aEvent)
    {
        aBuilder.addParagraph ("People invited to the event:");
        for (Invitation i : aEvent.getInvitations ().getElements ())
        {
            i.generateNameText (aBuilder);
            aBuilder.addBreak ();
        }
        aBuilder.addText ("Total price: ");
        aBuilder.addText (aEvent.getTotalPriceAsString ());
        aBuilder.addText ("   Per guest: ");
        aBuilder.addText (aEvent.getPricePerGuestAsString ());
    }

    public static void generateLocationInterestList (HtmlBuilder aBuilder, Location aLocation, Event aEvent)
    {
        boolean title = false;
        for (LocationInterest li : aLocation.getInterests ().getElements ())
        {
            if (aEvent == null || aEvent.locateInvitationForGuest (li.getGuestId ()) == null || !aEvent.locateInvitationForGuest (li.getGuestId ()).isInvited ())
            {
                // We know here that there is no existing invitation for the
                // guest.

                if (li.isInterested ())
                {
                    // Either the guest is interested by an invitation, or we
                    // are creating a new event.

                    if (!title)
                    {
                        title = true;
                        if (aEvent == null)
                        {
                            aBuilder.addParagraph ("Select more people to invite:");
                        }
                        else
                        {
                            aBuilder.addParagraph ("Select the people you want to invite:");
                        }
                    }

                    // Propose auto-invitation only if we are creating a new
                    // event, and the guest is interested.

                    aBuilder.addCheckBox ("INV_" + li.getGuest ().getIdAsString (), li.isAutoInvited () && aEvent == null);
                    li.generateNameText (aBuilder);
                    aBuilder.addBreak ();
                }
            }
        }
    }

    public static void generateNewInvitationList (HtmlBuilder aBuilder)
    {
        aBuilder.addParagraph ("Enter the e-mail address & name of more people you want to invite:");
        for (int i = 0; i < 4; i++)
        {
            aBuilder.addTextField ("NEWEMAIL_" + Long.toString (i), "");
            aBuilder.addTextField ("NEWNAME_" + Long.toString (i), "");
            aBuilder.addBreak ();
        }
    }
}
